const webpack = require('webpack')
const path = require('path')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const VueLoaderPlugin = require('vue-loader/lib/plugin-webpack4')
const NODE_ENV = process.env.NODE_ENV
console.log( NODE_ENV)
module.exports = {
  mode: NODE_ENV,
  entry: './src/main.js',
  resolve: {
    alias: { vue: 'vue/dist/vue.js' }
  },
  module: {
    rules: [
      {
        test: /\.(js|vue)$/,
        loader: 'eslint-loader',
        enforce: 'pre', //优先执行
        exclude: /node_modules/,
        options: {
          fix: true 
        }
      },
      {
        test: /\.vue$/,
        loader: 'vue-loader',
      },
      {
        test: /\.js$/,
        exclude: file => (
          /node_modules/.test(file) &&
          !/\.vue\.js/.test(file)
        ),
        loader: 'babel-loader',
        // 预设： 怎样的兼容性处理
        options: {
          presets: [
            [
              '@babel/preset-env',
              {
                //按需加载
                useBuiltIns: 'usage',
                // 指定core-js版本
                corejs: {
                  version: 3
                },
                // 指定兼容版本
                targets: {
                  chrome: '60',
                  firefox: '60',
                  ie: '9',
                  safari: '10',
                  edge: '17'
                }
              }
            ]
          ],
          // 开启babel缓存
          // 第二次构建是，会读取之前的缓冲
          cacheDirectory: true
        }
      },
      {
        test: /\.(less|css)$/,
        use: [
          NODE_ENV === 'development' ? 'vue-style-loader' : MiniCssExtractPlugin.loader,
          'css-loader',
          'postcss-loader',
          'less-loader'
        ],
      },
      {
        test: /\.(jpg|png|gif|ico)$/,
        loader: 'url-loader',
        options: {
          limit: 1 * 1024,
          esModule: false, // 老版本切换es6，处理img的src引入问题 ‘可隐藏新版好像不用它’
          name: '[name].[hash:10].[ext]', //取图片的前10为，ext取图片的原扩展名
          outputPath: 'image'
        },
      },
    ]
  },
  plugins: [
    new VueLoaderPlugin(),
    // new CopyWebpackPlugin([{ from: './public', to: path.resolve(__dirname,'dist') }],),
    new CopyWebpackPlugin({
      patterns:[
          { 
            from: path.resolve(__dirname, 'public'), 
          },
        ],
    })
  ]
}